Ext.onReady(function(){ 

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var record_start = 0; 

var sm = new Ext.grid.CheckboxSelectionModel();
var cm=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",
width:40, 
renderer:function(value,metadata,record,rowIndex){ 
return record_start + 1 + rowIndex; 
} 
}),
{header:'版本流水号',dataIndex:'mainScaleId',width:100},
{header:'版本名称',dataIndex:'mainScaleName',width:100},
{header:'版本号',dataIndex:'mainScaleVersion',width:100}, 
{header:'生效日期',dataIndex:'startDate',width:100},
{header:'失效日期',dataIndex:'endDate',width:100},
{header:'调整日志',dataIndex:'updateLog',width:100},
{header:'管理人',dataIndex:'inputUserId',width:100},
{header:'更新日期',dataIndex:'updateDate',width:100},
{header:'版本状态',dataIndex:'mainScaleState',width:100}

]); 

var ds = new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'model/listSortedMainScale.do',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ 
totalProperty:'total',  
root:'resultData'},[
{name:'mainScaleId'}, 
{name:'mainScaleName'},  
{name:'mainScaleVersion'},
{name:'startDate'},
{name:'endDate'},
{name:'updateLog'},
{name:'inputUserId'},
{name:'updateDate'},
{name:'mainScaleState'}
]) 
}); 
ds.load({params:{start:0,limit:20}}); 

var conditionStore = new Ext.data.SimpleStore({
   fields : ['id', 'text'],
   data : [['modelId', '客户编号'], ['modelKind1', '打分卡大类'], ['modelKind2', '打分卡小类']]
});

var grid=new Ext.grid.GridPanel({ 
id:'myCustomers',
renderTo:"customer_list", 
stripeRows:true,//斑马线效果
title:"主标尺——版本列表", 
height:500, 
cm:cm,
store: ds, 
loadMask: { msg: "加载中..." }, 
viewConfig: {   
                            forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 20,   
                store: ds,
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据" ,
                doLoad : function(start){ 
						 	record_start = start; 
							var o = {}, pn = this.getParams(); 
						    o[pn.start] = start; 
						    o[pn.limit] = this.pageSize; 
							this.store.load({params:o}); 
     			}   
            }),
 tbar: [
 {text:'新增', handler: addMS},
 '-',        
 {text:'查看/编辑', handler: msDetail},
 '-',
  {text:'生效', handler: setEffect},
 '-',
  {text:'发布', handler: setRelease},
 '-',
   {text:'设为未发布', handler: setNotRelease},
 '-',
   {text:'删除', handler: deleteModel}
 ]              
});


var addCustTypeStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=CustType',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) ,
autoLoad:true
}); 

var addCustScopeStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=CustScope',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) ,
autoLoad:true
}); 


//新增版本
var addForm = new Ext.form.FormPanel({ 
id: 'addForm', 
width: 430, 
height: 220, 
//样式 
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 80, 
items: 
[ { xtype: 'fieldset', 
title: '版本信息', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[ { 
xtype: 'textfield', 
id:'mainScaleVersion',
name: 'mainScaleVersion', 
fieldLabel: '版本号', 
emptyText: '必填', 
blankText: '版本号不能为空', 
allowBlank: false, 
width: 280,
maxLength: 20, 
maxLengthText: '版本号不能超过20个字符', 
},{ 
xtype: 'textfield', 
id:'mainScaleName',
name: 'mainScaleName', 
fieldLabel: '版本名称', 
emptyText: '必填', 
blankText: '版本名称不能为空', 
allowBlank: false, 
width: 280,
maxLength: 20, 
maxLengthText: '版本号不能超过20个字符', 
}
] 
}]
});

//新增版本窗口 
addWin = new Ext.Window({ 
id: 'addWin', 
title: '新增版本', 
width: 450, 
height: 230, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addForm, 
buttons: 
[  
{ text: '保存', id: 'btnSubmit' } ,
{ text: '关闭', handler: function() { Ext.getCmp('addWin').hide(); } }

] 
});

//调整原因
addUpdateForm = new Ext.form.FormPanel({ 
id: 'addUpdateForm', 
width: 480, 
height: 300, 
//样式 
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 80, 
items: 
[ { xtype: 'fieldset', 
title: '调整原因', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[ { 
xtype: 'textarea', 
id:'updateLog',
name: 'updateLog', 
fieldLabel: '调整原因', 
emptyText: '请输入调整原因', 
maxLength:100,
maxLengthText:'最多只允许输入100个汉字',
height:100,
allowBlank: false, 
blankText:'请输入调整原因',
anchor: '98%' 
}
] 
}]
});

//Update窗口 
addUpdateWin = new Ext.Window({ 
id: 'addUpdateWin', 
title: '版本管理', 
width: 500, 
height: 320, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addUpdateForm, 
buttons: 
[  
{ text: '保存', id: 'btnUpdateSubmit' } ,
{ text: '关闭', handler: function() { Ext.getCmp('addUpdateWin').hide(); } }

] 
});



function findCustomer(){

var condition = Ext.getCmp('condition').getValue();
var keyword = Ext.getCmp('keyword').getValue();

if(condition == '' || condition == null){
	Ext.Msg.alert('提示', '请选择查询条件！'); 
}
else{

	ds.baseParams={condition:condition, keyword: keyword};
	ds.proxy = new Ext.data.HttpProxy({
					url:'rating/findCustomer.do',
					method:'POST'
				}); 
	ds.reader = new Ext.data.JsonReader({ 
						totalProperty:'total',  
						root:'resultData'},[
						{name:'custId', mapping: 'custInfo.custId'}, 
						{name:'custName', mapping: 'custInfo.custName'},  
						{name:'industryType', mapping: 'custInfo.industryType'},
						{name:'defaultSta', mapping: 'custInfo.defaultSta'},
						{name:'exposureType', mapping: 'custInfo.exposureType'},
						{name:'phaseLevel', mapping: 'custPD.phaseLevel'},
						{name:'phaseDate', mapping:' custPD.phaseDate'},
						{name:'endDate'},
						{name:'firstUserId', mapping: 'custPD.firstUserId'},
						{name:'phaseStat', mapping: 'custPD.phaseStat'}
					]);
					   
	ds.load({params:{start:0,limit:20}});
	record_start = 0;  	
}
}

function callBack(){

Ext.getCmp('condition').clearValue();
Ext.getCmp('keyword').setValue('');
	ds.proxy = new Ext.data.HttpProxy({
					url:'rating/listSortedPD.do',
					method:'POST'
				}); 
	ds.reader = new Ext.data.JsonReader({ 
						totalProperty:'total',  
						root:'resultData'},[
						{name:'custId', mapping: 'custInfo.custId'}, 
						{name:'custName', mapping: 'custInfo.custName'},  
						{name:'industryType', mapping: 'custInfo.industryType'},
						{name:'defaultSta', mapping: 'custInfo.defaultSta'},
						{name:'exposureType', mapping: 'custInfo.exposureType'},
						{name:'phaseLevel', mapping: 'custPD.phaseLevel'},
						{name:'phaseDate', mapping:' custPD.phaseDate'},
						{name:'endDate'},
						{name:'firstUserId', mapping: 'custPD.firstUserId'},
						{name:'phaseStat', mapping: 'custPD.phaseStat'}
					]);   
	ds.load({params:{start:0,limit:20}});
	record_start = 0;

}

function listCustPD(){
	if (grid.getSelectionModel().getSelections()[0] == undefined) { 
		Ext.Msg.alert("提示", "请选中要操作的项！"); 
	}
	else{
		var selectedModelId = grid.getSelectionModel().getSelections()[0].data.modelId;
		window.location.href="model/viewDetail?modelId="+selectedModelId;
	}
}

function addMS(){
	Ext.getCmp("addWin").show();
	Ext.getCmp("addForm").form.getEl().dom.reset(); 
	//查重
	Ext.getCmp("addWin").buttons[0].handler = function(){
		var version = Ext.getCmp('mainScaleVersion').getValue();
		var name = Ext.getCmp('mainScaleName').getValue(); 
		
		if(addForm.form.isValid()){
				Ext.Ajax.request({
				  url:'model/checkMainScale.do',
				   params : {mainScaleVersion : version, mainScaleName:name},
				  method:'GET',
				  success:function(response){
				 var isExit = response.responseText;
				 if (isExit==1){
				 	Ext.MessageBox.alert("提示","当前版本已存在，请重新输入！");
				 }
				 else if (isExit == 0){
				 	var addForm = Ext.getCmp("addForm");
				 	var addCustForm=addForm.getForm().getEl().dom;
					// Ext.MessageBox.alert("提示","恭喜你，当前版本不存在！");
				 	addCustForm.action = 'model/addMainscale.do';
				 	addCustForm.method = 'POST';
				 	addCustForm.submit();
				 
				 	Ext.MessageBox.alert("提示","添加成功");
				 	Ext.getCmp("addForm").form.reset(); 
				 	Ext.getCmp("addWin").hide(); 
				
				 	grid.store.reload();
				 
				
				 }
				   // window.location.href="view/customer/CheckCustomer_General.jsp";
				  },failure:function(){
				    Ext.MessageBox.alert("请求异常！");
				  }
				});  
			}
		
	};
}

function msDetail(){
	if (grid.getSelectionModel().getSelections()[0] == undefined) { 
		Ext.Msg.alert("提示", "请选中要操作的项！"); 
	}
	else{
		var selectedId = grid.getSelectionModel().getSelections()[0].data.mainScaleId;
		window.location.href="model/viewMSDetail.do?mainScaleId="+selectedId;
	}
}

//设置生效
function setEffect(){
	if (grid.getSelectionModel().getSelections()[0] == undefined) { 
		Ext.Msg.alert("提示", "请选中要操作的项！"); 
	}
	else{
		var addForm = Ext.getCmp("addUpdateForm");
		var selectedId = grid.getSelectionModel().getSelections()[0].data.mainScaleId;
		var selectedState = grid.getSelectionModel().getSelections()[0].data.mainScaleState;
		if(selectedState == '未发布'){
			Ext.Msg.alert("提示", "当前版本未发布，无法将其置为生效状态！"); 
		}
		else if (selectedState == '失效'){
			Ext.Msg.alert("提示", "当前版本已失效，无法将其置为生效状态！");
		}
		else if (selectedState == '生效'){
			Ext.Msg.alert("提示", "当前版本已生效，无需重复操作！");
		} 
		else{
		Ext.Msg.confirm("Confirm",'确定将此版本设为生效状态吗？',function(bool){
					if(bool=='yes'){
						Ext.getCmp("addUpdateWin").show();
						Ext.getCmp("addUpdateForm").form.getEl().dom.reset(); 

						Ext.getCmp("addUpdateWin").buttons[0].handler = function(){
							
								if(addForm.form.isValid()){
									
							 	addForm.form.doAction('submit', { 
										url : 'model/effectMainScale.do',
										params : {
											mainScaleId : selectedId
										},
										method : 'POST',
										success : function() {
											Ext.Msg.alert("提示", "操作成功！");
											grid.store.reload();
											Ext.getCmp("addUpdateForm").form.reset(); 
											Ext.getCmp("addUpdateWin").hide(); 
										},
										failure : function() {
											Ext.MessageBox.alert("请求异常！");
										}
									});
							 	}
							
						};
						return true;
					}
					else{
						return false;	
					}
				}); 
		}
	}
}

//设为发布
function setRelease(){
	if (grid.getSelectionModel().getSelections()[0] == undefined) { 
		Ext.Msg.alert("提示", "请选中要操作的项！"); 
	}
	else{
		var addForm = Ext.getCmp("addUpdateForm");
		var selectedId = grid.getSelectionModel().getSelections()[0].data.mainScaleId;
		var selectedState = grid.getSelectionModel().getSelections()[0].data.mainScaleState;
		
		if(selectedState == '发布'){
			Ext.Msg.alert("提示", "当前版本已发布，无需重复操作！"); 
		}
		else if (selectedState == '失效'){
			Ext.Msg.alert("提示", "当前版本已失效，无法进行该操作！");
		}
		else if (selectedState == '生效'){
			Ext.Msg.alert("提示", "当前版本已生效，无法进行该操作！");
		} 
		else{
			
			Ext.Msg.confirm("Confirm",'确定发布该版本吗？',function(bool){
					if(bool=='yes'){
						Ext.getCmp("addUpdateWin").show();
						Ext.getCmp("addUpdateForm").form.getEl().dom.reset(); 

						Ext.getCmp("addUpdateWin").buttons[0].handler = function(){
							
								if(addForm.form.isValid()){
							 	addForm.form.doAction('submit', { 
										url : 'model/releaseMainScale.do',
										params : {
											mainScaleId : selectedId
										},
										method : 'POST',
										success : function() {
											Ext.Msg.alert("提示", "操作成功！");
											grid.store.reload();
											Ext.getCmp("addUpdateForm").form.reset(); 
											Ext.getCmp("addUpdateWin").hide(); 
										},
										failure : function() {
											Ext.MessageBox.alert("请求异常！");
										}
									});
							 	}
							
						};
						return true;
					}
					else{
						return false;	
					}
				}); 
		}
	}
}

//设为未发布
function setNotRelease(){
		if (grid.getSelectionModel().getSelections()[0] == undefined) { 
		Ext.Msg.alert("提示", "请选中要操作的项！"); 
	}
	else{
		var addForm = Ext.getCmp("addUpdateForm");
		var selectedId = grid.getSelectionModel().getSelections()[0].data.mainScaleId;
		var selectedState = grid.getSelectionModel().getSelections()[0].data.mainScaleState;
		
		if(selectedState == '未发布'){
			Ext.Msg.alert("提示", "当前已是未发布状态！"); 
		}
		else if (selectedState == '失效'){
			Ext.Msg.alert("提示", "当前版本已失效，无法将其置为未发布状态！");
		}
		else if (selectedState == '生效'){
			Ext.Msg.alert("提示", "当前版本已生效，无法将其置为未发布状态！");
		} 
		else{
				Ext.Msg.confirm("Confirm",'确定将此版本设为未发布状态吗？',function(bool){
					if(bool=='yes'){
						Ext.getCmp("addUpdateWin").show();
						Ext.getCmp("addUpdateForm").form.getEl().dom.reset(); 

						Ext.getCmp("addUpdateWin").buttons[0].handler = function(){
							
								if(addForm.form.isValid()){
							 	addForm.form.doAction('submit', { 
										url : 'model/notReleaseMainScale.do',
										params : {
											mainScaleId : selectedId
										},
										method : 'POST',
										success : function() {
											Ext.Msg.alert("提示", "操作成功！");
											grid.store.reload();
											Ext.getCmp("addUpdateForm").form.reset(); 
											Ext.getCmp("addUpdateWin").hide(); 
										},
										failure : function() {
											Ext.MessageBox.alert("请求异常！");
										}
									});
							 	}
							
						};
						
						return true;
					}
					else{
						return false;	
					}
				}); 
		}
	}
}

function deleteModel(){
	if (grid.getSelectionModel().getSelections()[0] == undefined) { 
		Ext.Msg.alert("提示", "请选中要操作的项！"); 
	}
	else{
		var addForm = Ext.getCmp("addUpdateForm");
		var selectedId = grid.getSelectionModel().getSelections()[0].data.modelId;
		var selectedState = grid.getSelectionModel().getSelections()[0].data.mainScaleState;
		
		
		if(selectedState=='未发布'){
			Ext.Msg.confirm("Confirm",'确定删除？',function(bool){
					if(bool=='yes'){
						var selectedModelId = grid.getSelectionModel().getSelections()[0].data.modelId;
						window.location.href="model/deleteModel.do?modelId="+selectedModelId;
						return true;
					}
					else{
						return false;	
					}
				}); 
		}
		else if(selectedState=='发布'){
			Ext.Msg.confirm("Confirm",'确定删除？',function(bool){
					if(bool=='yes'){
						Ext.getCmp("addUpdateWin").show();
						Ext.getCmp("addUpdateForm").form.getEl().dom.reset(); 

						Ext.getCmp("addUpdateWin").buttons[0].handler = function(){
							
								if(addForm.form.isValid()){
							 	addForm.form.doAction('submit', { 
										url : 'model/deleteReleasedModel.do',
										params : {
											modelId : selectedId
										},
										method : 'POST',
										success : function() {
											Ext.Msg.alert("提示", "操作成功！");
											grid.store.reload();
											Ext.getCmp("addUpdateForm").form.reset(); 
											Ext.getCmp("addUpdateWin").hide(); 
										},
										failure : function() {
											Ext.MessageBox.alert("请求异常！");
										}
									});
							 	}
							
						};
						
						return true;
					}
					else{
						return false;	
					}
				}); 
		}
		else{
			Ext.Msg.alert("提示", selectedState+"状态下不允许删除！"); 
		}
	}
}



function rowdblclickFn(grid, rowIndex, e){//双击事件  
	var row = grid.getStore().getAt(rowIndex); 
	var selectedModelId = row.get("mainScaleId");
		window.location.href="model/viewMSDetail.do?mainScaleId="+selectedModelId;
}   

grid.addListener('rowdblclick',rowdblclickFn);

}); 